#!/usr/bin/perl

use lib::abs qw( .. ../../base_lib );
use JIP::ToolSet;

use Readonly qw( Readonly );
use English qw( -no_match_vars );
use Carp qw( croak );
use Test::More tests => 19;

Readonly my $INPUT_FILE => './fake_input_file';
Readonly my $OUTPUT_DIR => './output_dir';

BEGIN { use_ok 'Filter'; }
require_ok 'Filter';

can_ok(
    'Filter',
    qw(
        new
        init
        filterLoop
        hostId
        hostIp
        inputFile
        outputDir
        inputFileHandler
        isMemoizeEnabled
        _getOutputFileLocation
        _getHandlerName
        _makeInputHandle
        _makeOutputHandlers
    )
);

{
    makeFikeFile();
    mkdir( $OUTPUT_DIR ) or croak $OS_ERROR;

    my $f = Filter->new(
        host_id      => 42,
        host_ip      => '127.0.0.1',
        input_file   => $INPUT_FILE,
        output_dir   => $OUTPUT_DIR,
        enable_cache => 1,
    );

    isa_ok $f, 'Filter';
    isa_ok $f, 'Class::Base';
    isa_ok $f, 'Class::Accessor::Fast';

    isa_ok $f->inputFileHandler, 'IO::File';

    try {
        $f->hostId('tratata');
    }
    catch {
        like(
            $ARG,
            qr/cannot alter the value of/,
            'r.o. access to hostId'
        );
    };

    try {
        $f->hostIp('tratata');
    }
    catch {
        like(
            $ARG,
            qr/cannot alter the value of/,
            'r.o. access to hostIp'
        );
    };

    try {
        $f->inputFile('tratata');
    }
    catch {
        like(
            $ARG,
            qr/cannot alter the value of/,
            'r.o. access to inputFile'
        );
    };

    try {
        $f->outputDir('tratata');
    }
    catch {
        like(
            $ARG,
            qr/cannot alter the value of/,
            'r.o. access to outputDir'
        );
    };

    try {
        $f->inputFileHandler('tratata');
    }
    catch {
        like(
            $ARG,
            qr/cannot alter the value of/,
            'r.o. access to inputFileHandler'
        );
    };

    try {
        $f->isMemoizeEnabled('tratata');
    }
    catch {
        like(
            $ARG,
            qr/cannot alter the value of/,
            'r.o. access to isMemoizeEnabled'
        );
    };

    is(
        $f->_getOutputFileLocation('video'),
        './output_dir/ftp_video_127.0.0.1.txt',
        '_getOutputFileLocation'
    );

    is( Filter::_getHandlerName('video'), 'outputVideo', '_getHandlerName' );

    unlink glob( $OUTPUT_DIR .'/*.txt' );
    rmdir( $OUTPUT_DIR )  or croak $OS_ERROR;
    unlink( $INPUT_FILE ) or croak $OS_ERROR;
}

{
    try {
        Filter->new;
    }
    catch {
        like(
            $ARG,
            qr/^required parameter "host_id" not defined at/,
            'new() without "host_id"'
        );
    };

    try {
        Filter->new( host_id => 1 );
    }
    catch {
        like(
            $ARG,
            qr/^required parameter "host_ip" not defined at/,
            'new() without "host_ip"'
        );
    };

    try {
        Filter->new( host_id => 1, host_ip => 1 );
    }
    catch {
        like(
            $ARG,
            qr/^required parameter "input_file" not defined at/,
            'new() without "input_file"'
        );
    };

    try {
        Filter->new( host_id => 1, host_ip => 1, input_file => 1 );
    }
    catch {
        like(
            $ARG,
            qr/^required parameter "output_dir" not defined at/,
            'new() without "output_dir"'
        );
    };
}

sub makeFikeFile
{
    open( FAKE, '>', $INPUT_FILE ) or croak $OS_ERROR;
    close( FAKE ) or croak $OS_ERROR;
}
